import CryptoJS from 'crypto-js'
import { getSecretKey } from '../../config/env'

export const shortAddress = (address: string) => {
  if (!address) return ''
  const len = address.length
  return `${address.slice(0, 5)}...${address.slice(len - 4, len)}`
}

const SECRET_KEY = CryptoJS.enc.Utf8.parse(getSecretKey())

// 加密函数
export const encryptData = (originalData: string) => {
  // 使用AES-256加密
  const encrypted = CryptoJS.AES.encrypt(originalData, SECRET_KEY, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7,
  })
  return encrypted.toString()
}

// 消息中链接处理
export const formatText = (text: string) => {
  const reg = /(https?:\/\/[\w\-.]+(:\d+)?(\/[\w\-_.]*)*(\?[\w=&-]*)?)/g
  const nextText = text
    .trim()
    .replace(/(https?:\/\/[\w\-.]+(:\d+)?)/g, ' $1')
    .replace(
      reg,
      '<a class="break-all underline cursor-pointer" style="color: #00aff0;" target="_blank" href=\'$1\'>$1</a>',
    )
    .replace(/\n/g, '<br />')
  return nextText
}
